package com.hyt.it.ogt.ykcj.service.exam.impl;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.hyt.it.ogt.ykcj.common.enums.BizErrorCode;
import com.hyt.it.ogt.ykcj.common.exception.CustomException;
import com.hyt.it.ogt.ykcj.common.utils.DateUtils;
import com.hyt.it.ogt.ykcj.common.utils.IdUtils;
import com.hyt.it.ogt.ykcj.common.utils.SecurityUtils;
import com.hyt.it.ogt.ykcj.common.utils.bean.BeanUtils;
import com.hyt.it.ogt.ykcj.domain.exam.ExamPostScoreLineSetting;
import com.hyt.it.ogt.ykcj.domain.exam.ExamPostScoreLineSettingSub;
import com.hyt.it.ogt.ykcj.domain.vo.ExamPostScoreLineSettingVo;
import com.hyt.it.ogt.ykcj.mapper.exam.ExamPostScoreLineSettingMapper;
import com.hyt.it.ogt.ykcj.mapper.exam.ExamPostScoreLineSettingSubMapper;
import com.hyt.it.ogt.ykcj.service.exam.IExamPostScoreLineSettingService;
import com.hyt.it.ogt.ykcj.service.exam.IExamSubjectInfoService;

/**
 * 招考职位分数线设置Service业务层处理
 * 
 * @author admin
 * @date 2021-09-09
 */
@Service
public class ExamPostScoreLineSettingServiceImpl implements IExamPostScoreLineSettingService
{
    @Autowired
    private ExamPostScoreLineSettingMapper examPostScoreLineSettingMapper;

    @Autowired
    private ExamPostScoreLineSettingSubMapper examPostScoreLineSettingSubMapper;

    @Autowired
    private IExamSubjectInfoService examSubjectInfoService;

    @Autowired
    private ChangedExamInfoServiceImpl changedExamInfoService;

    /**
     * 查询招考职位分数线设置
     *
     * @param id 招考职位分数线设置ID
     * @return 招考职位分数线设置
     */
    @Override
    public ExamPostScoreLineSetting selectExamPostScoreLineSettingById(String id)
    {
        return examPostScoreLineSettingMapper.selectExamPostScoreLineSettingById(id);
    }

    /**
     * 查询招考职位分数线设置列表
     *
     * @param examPostScoreLineSetting 招考职位分数线设置
     * @return 招考职位分数线设置
     */
    @Override
    public List<ExamPostScoreLineSetting> selectExamPostScoreLineSettingList(ExamPostScoreLineSetting examPostScoreLineSetting)
    {
        return examPostScoreLineSettingMapper.selectExamPostScoreLineSettingList(examPostScoreLineSetting);
    }

    /**
     * 新增招考职位分数线设置
     *
     * @param examPostScoreLineSetting 招考职位分数线设置
     * @return 结果
     */
    @Override
    public int insertExamPostScoreLineSetting(ExamPostScoreLineSetting examPostScoreLineSetting)
    {
        examPostScoreLineSetting.setCreateTime(DateUtils.getNowDate());
        return examPostScoreLineSettingMapper.insertExamPostScoreLineSetting(examPostScoreLineSetting);
    }

    /**
     * 修改招考职位分数线设置
     *
     * @param examPostScoreLineSetting 招考职位分数线设置
     * @return 结果
     */
    @Override
    public int updateExamPostScoreLineSetting(ExamPostScoreLineSetting examPostScoreLineSetting)
    {
        examPostScoreLineSetting.setUpdateTime(DateUtils.getNowDate());
        return examPostScoreLineSettingMapper.updateExamPostScoreLineSetting(examPostScoreLineSetting);
    }

    /**
     * 批量删除招考职位分数线设置
     *
     * @param ids 需要删除的招考职位分数线设置ID
     * @return 结果
     */
    @Override
    public int deleteExamPostScoreLineSettingByIds(String[] ids)
    {
        ExamPostScoreLineSetting examPostScoreLineSetting =  examPostScoreLineSettingMapper.selectExamPostScoreLineSettingById(ids[0]);
        //添加考试变更信息
        changedExamInfoService.addChangedExamInfo(examPostScoreLineSetting.getExamId());
        return examPostScoreLineSettingMapper.deleteExamPostScoreLineSettingByIds(ids);
    }

    /**
     * 删除招考职位分数线设置信息
     *
     * @param id 招考职位分数线设置ID
     * @return 结果
     */
    @Override
    public int deleteExamPostScoreLineSettingById(String id)
    {
        ExamPostScoreLineSetting examPostScoreLineSetting =  examPostScoreLineSettingMapper.selectExamPostScoreLineSettingById(id);
        //添加考试变更信息
        changedExamInfoService.addChangedExamInfo(examPostScoreLineSetting.getExamId());

        return examPostScoreLineSettingMapper.deleteExamPostScoreLineSettingById(id);
    }

    /**
     * 获取职位分数线设置选项列表
     * @param examId 考试ID
     * @param dictType 字典类型
     * @return 结果
     */
    @Override
    public List<Map<String,String>> getPostScoreLineSettingOption(String examId, String dictType) {
        return examPostScoreLineSettingMapper.getPostScoreLineSettingOption(examId, dictType);
    }

    /**
     * 获取职位数量
     * @param examPostScoreLineSetting 招考职位分数线设置
     * @return 职位数量
     */
    @Override
    public Integer getPostNumber(ExamPostScoreLineSetting examPostScoreLineSetting) {
        return examPostScoreLineSettingMapper.getPostNumber(examPostScoreLineSetting);
    }

    @Override
    @Transactional
    public Integer insertBatchExamPostScoreLineSetting(List<ExamPostScoreLineSettingVo> listVo) {
        List<Map<String,String>> mapList = examSubjectInfoService.getSettingScoreLineSubjectName(listVo.get(0).getExamId());
        Map<String,String> fullMarksMap = new HashMap<>();
        for (Map<String,String> map : mapList) {
            fullMarksMap.put(map.get("subjectId"), map.get("fullMarks"));
        }
        Integer i = 0;
        for (ExamPostScoreLineSettingVo vo : listVo) {
            ExamPostScoreLineSetting examPostScoreLineSetting = new ExamPostScoreLineSetting();
            BeanUtils.copyBeanProp(examPostScoreLineSetting, vo);
            if (StringUtils.isNotEmpty(examPostScoreLineSetting.getId())) {
                for (ExamPostScoreLineSettingSub sub : vo.getSubList()) {
                    this.checkPostScore(String.valueOf(fullMarksMap.get(sub.getSubjectId())), sub.getScore());
                }
                examPostScoreLineSetting.updateInfo(SecurityUtils.getUsername());
                examPostScoreLineSettingSubMapper.updateBatchExamPostScoreLineSettingSub(vo.getSubList());
                i += examPostScoreLineSettingMapper.updateExamPostScoreLineSetting(examPostScoreLineSetting);
            } else {
                String postScoreLineSettingId = IdUtils.generateId();
                examPostScoreLineSetting.setId(postScoreLineSettingId);
                examPostScoreLineSetting.createInfo(SecurityUtils.getUsername());
                examPostScoreLineSetting.updateInfo(SecurityUtils.getUsername());
                examPostScoreLineSetting.setDeptId(SecurityUtils.getLoginUser().getUser().getDeptId());
                for (ExamPostScoreLineSettingSub sub : vo.getSubList()) {
                    this.checkPostScore(String.valueOf(fullMarksMap.get(sub.getSubjectId())), sub.getScore());
                    sub.setId(IdUtils.generateId());
                    sub.setPostScoreLineSettingId(postScoreLineSettingId);
                }
                examPostScoreLineSettingSubMapper.insertBatchExamPostScoreLineSettingSub(vo.getSubList());
                try {
                    i += examPostScoreLineSettingMapper.insertExamPostScoreLineSetting(examPostScoreLineSetting);
                } catch (DuplicateKeyException e) { // SQLIntegrityConstraintViolationException
                    throw new CustomException(BizErrorCode.COM_E0005);
                }
            }
        }
        //添加考试变更信息
        changedExamInfoService.addChangedExamInfo(listVo.get(0).getExamId());
        return i;
    }

    private void checkPostScore(String fullMarks, String score) {
        BigDecimal fullMarksB = new BigDecimal(fullMarks);
        BigDecimal scoreB = new BigDecimal(score);
        if (scoreB.compareTo(fullMarksB) > 0) {
            throw new CustomException("分数线值不能高于科目满分，请检查!");
        }
    }

    @Override
    public List<ExamPostScoreLineSettingVo> getList(String examId) {
        return examPostScoreLineSettingMapper.getExamPostScoreLineSettingVoList(examId);
    }
}
